﻿@model BuildServer.Models.EditPropertyModel

@{
    ViewBag.Title = "Build Definition Properties";
    string lastGroup = null;
    string[] readOnly = new[] { "build_label" };
}

<h2 class="title">@ViewBag.Title - @Model.Name</h2>

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    @Html.HiddenFor(model => model.id)
    @Html.HiddenFor(model => model.Name)
    
    for (int i = 0; i < Model.Properties.Count; i++)
    {
        @Html.HiddenFor(m => m.Properties[i].Name)
        @Html.HiddenFor(m => m.Properties[i].Group)
        @Html.HiddenFor(m => m.Properties[i].Label)
    }
    <table class="editor-table">
        @for (int i = 0; i < Model.Properties.Count; i++)
        {
            var editProperty = Model.Properties[i];
            if (editProperty.Group != lastGroup)
            {
                lastGroup = editProperty.Group;
                string cssClass = i == 0 ? "group" : "top-border";
            <tr>
                <th colspan="2" class="@cssClass">@Html.Label(editProperty.Group)</th>
            </tr>
                }
            <tr>
                <td class="editor-label">@Html.Label(editProperty.Label)</td>
                <td class="editor-field">
                    @if (readOnly.Contains(editProperty.Name))
                    {
                        @Html.HiddenFor(m => m.Properties[i].Value)
                        @Html.DisplayFor(m => m.Properties[i].Value)
                    }
                    else if (editProperty.Options != null && editProperty.Options.Length > 0)
                    {
                        @EditOptions(string.Format("Properties[{0}].Value", i), editProperty)
                    }
                    else {
                        @Html.EditorFor(m => m.Properties[i].Value)
                        @Html.ValidationMessageFor(m => m.Properties[i].Value)
                    }
                </td>
            </tr>
            }
    </table>
    <p>
        <input type="submit" value="Save" /> |
        @Html.ActionLink("Back to List", "Index", "Definition")
    </p>
}

@helper EditOptions(string inputName, BuildServer.Models.EditProperty editProperty)
{
    foreach (string option in editProperty.Options)
    {
        string inputId = editProperty.Name + "_" + option;
        string isChecked = (option == editProperty.Value) ? "checked" : null;
        <input id="@inputId" name="@inputName" type="radio" value="@option" checked="@isChecked" />
        <label for="@inputId">@option</label>
    }
}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
